package com.ssy.lingxi.member.merchant.serviceimpl.mobile;

import cn.hutool.core.util.StrUtil;
import com.ssy.lingxi.common.constant.basic.EnableDisableStatus;
import com.ssy.lingxi.common.constant.member.MemberTypeEnum;
import com.ssy.lingxi.common.constant.order.OrderSourceKindEnum;
import com.ssy.lingxi.common.constant.order.OrderTypeEnum;
import com.ssy.lingxi.common.model.bo.MemberAndRoleIdBO;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberTradeCommentSubmitDetailVO;
import com.ssy.lingxi.member.merchant.entity.MemberOrderCommentDO;
import com.ssy.lingxi.member.merchant.entity.MemberOrderProductCommentDO;
import com.ssy.lingxi.member.merchant.entity.MemberRelationDO;
import com.ssy.lingxi.member.merchant.entity.MemberTradeCommentHistoryDO;
import com.ssy.lingxi.member.merchant.api.constant.MemberRelationTypeEnum;
import com.ssy.lingxi.member.merchant.model.constant.MemberTradeTypeEnum;
import com.ssy.lingxi.member.merchant.model.vo.lifecycle.request.CommonIdVO;
import com.ssy.lingxi.member.merchant.model.vo.mobile.request.MobileMemberTradeCommentSaveVO;
import com.ssy.lingxi.member.merchant.model.vo.mobile.response.*;
import com.ssy.lingxi.member.merchant.repository.MemberOrderCommentRepository;
import com.ssy.lingxi.member.merchant.repository.MemberOrderProductCommentRepository;
import com.ssy.lingxi.member.merchant.repository.MemberRelationRepository;
import com.ssy.lingxi.member.merchant.repository.MemberTradeCommentHistoryRepository;
import com.ssy.lingxi.member.merchant.service.mobile.IMobileMemberCommentService;
import com.ssy.lingxi.member.merchant.serviceimpl.web.BaseMemberCommentServiceImpl;
import com.ssy.lingxi.order.api.feign.OrderFeignService;
import com.ssy.lingxi.order.api.model.vo.request.OrderFeignIdsVO;
import com.ssy.lingxi.order.api.model.vo.response.OrderVendorLogoFeignVO;
import com.ssy.lingxi.platform.template.api.feign.inner.MemberShopInnerControllerFeign;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * App - 会员评价服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/2/24
 */
@Service
public class MobileMemberCommentServiceImpl extends BaseMemberCommentServiceImpl implements IMobileMemberCommentService {

    @Resource
    private MemberOrderCommentRepository memberOrderCommentRepository;

    @Resource
    private MemberOrderProductCommentRepository memberOrderProductCommentRepository;

    @Resource
    private MemberTradeCommentHistoryRepository memberTradeCommentHistoryRepository;

    @Resource
    private MemberRelationRepository memberRelationRepository;

    @Resource
    private MemberShopInnerControllerFeign memberShopInnerControllerFeign;

    @Resource
    private OrderFeignService orderFeignService;

    @Override
    public Wrapper<PageData<MobileWaitCommentOrderProductPageVO>> pageWaitOrderComment(UserLoginCacheDTO loginUser, PageVO pageVO) {
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize(), Sort.by("order.createTime").descending());
        Page<MemberOrderProductCommentDO> pageList = memberOrderProductCommentRepository.findAll((Specification<MemberOrderProductCommentDO>) (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            // 未评价
            predicateList.add(cb.equal(root.get("buyerCommentStatus"), EnableDisableStatus.DISABLE.getCode()));

            Join<Object, Object> orderJoin = root.join("order", JoinType.LEFT);
            predicateList.add(cb.equal(orderJoin.get("buyerMemberId"), loginUser.getMemberId()));
            predicateList.add(cb.equal(orderJoin.get("buyerRoleId"), loginUser.getMemberRoleId()));

            // 非SRM订单(SRM订单含有物料,不能评价)
            List<Integer> orderSourceKinds = Arrays.asList(
                    OrderSourceKindEnum.BUYER.getCode(),
                    OrderSourceKindEnum.B2B.getCode(),
                    OrderSourceKindEnum.MOBILE_BUYER.getCode(),
                    OrderSourceKindEnum.RIGHT_POINT.getCode(),
                    OrderSourceKindEnum.AGENT.getCode()
            );
            predicateList.add(cb.in(orderJoin.get("orderKind")).value(orderSourceKinds));

            // app只显示非合同类型的订单
            List<Integer> orderTypeList = Arrays.asList(
                    OrderTypeEnum.INQUIRY_TO_PURCHASE.getCode(),
                    OrderTypeEnum.NEED_TO_PURCHASE.getCode(),
                    OrderTypeEnum.SPOT_PURCHASING.getCode(),
                    OrderTypeEnum.COLLECTIVE_PURCHASE.getCode(),
                    OrderTypeEnum.CHANNEL_STRAIGHT_MINING.getCode(),
                    OrderTypeEnum.CHANNEL_SPOT.getCode(),
                    OrderTypeEnum.CREDITS_EXCHANGE.getCode(),
                    OrderTypeEnum.CHANNEL_POINT_EXCHANGE.getCode());
            predicateList.add(cb.in(orderJoin.get("orderType")).value(orderTypeList));

            Predicate[] p = new Predicate[predicateList.size()];
            return query.where(predicateList.toArray(p)).getRestriction();
        }, page);

        List<MobileWaitCommentOrderProductPageVO> resultList = pageList.stream().map(e -> {
            MobileWaitCommentOrderProductPageVO waitCommentOrderPageVO = new MobileWaitCommentOrderProductPageVO();
            waitCommentOrderPageVO.setOrderProductId(e.getId());
            waitCommentOrderPageVO.setProductId(e.getProductId());
            waitCommentOrderPageVO.setSkuId(e.getSkuId());
            waitCommentOrderPageVO.setProductNo(e.getProductNo());
            waitCommentOrderPageVO.setName(e.getName());
            waitCommentOrderPageVO.setCategory(e.getCategory());
            waitCommentOrderPageVO.setBrand(e.getBrand());
            waitCommentOrderPageVO.setSpec(e.getSpec());
            waitCommentOrderPageVO.setUnit(e.getUnit());
            waitCommentOrderPageVO.setLogo(e.getLogo());
            waitCommentOrderPageVO.setPrice(e.getPrice());
            waitCommentOrderPageVO.setQuantity(e.getQuantity());
            waitCommentOrderPageVO.setAmount(e.getAmount());
            waitCommentOrderPageVO.setBuyerCommentStatus(e.getBuyerCommentStatus());
            return waitCommentOrderPageVO;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<MobileCommentOrderProductVO> getMemberOrderComment(UserLoginCacheDTO loginUser, CommonIdVO commonIdVO) {
        MemberOrderProductCommentDO orderProductCommentDO = memberOrderProductCommentRepository.findById(commonIdVO.getId()).orElse(null);
        if (Objects.isNull(orderProductCommentDO)) {
            return Wrapper.fail(ResponseCode.MC_MS_RECORDS_DON_T_EXIST);
        }

        MobileCommentOrderProductVO response = new MobileCommentOrderProductVO();
        response.setOrderProductId(orderProductCommentDO.getId());
        response.setProductId(orderProductCommentDO.getProductId());
        response.setSkuId(orderProductCommentDO.getSkuId());
        response.setProductNo(orderProductCommentDO.getProductNo());
        response.setName(orderProductCommentDO.getName());
        response.setCategory(orderProductCommentDO.getCategory());
        response.setBrand(orderProductCommentDO.getBrand());
        response.setSpec(orderProductCommentDO.getSpec());
        response.setUnit(orderProductCommentDO.getUnit());
        response.setLogo(orderProductCommentDO.getLogo());
        response.setPrice(orderProductCommentDO.getPrice());
        response.setQuantity(orderProductCommentDO.getQuantity());
        response.setAmount(orderProductCommentDO.getAmount());

        return Wrapper.success(response);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> saveMemberComment(UserLoginCacheDTO loginUser, MobileMemberTradeCommentSaveVO memberTradeCommentSaveVO) {
        MemberOrderProductCommentDO orderProductCommentDO = memberOrderProductCommentRepository.findById(memberTradeCommentSaveVO.getOrderProductId()).orElse(null);
        if (Objects.isNull(orderProductCommentDO)) {
            return Wrapper.fail(ResponseCode.MC_MS_RECORDS_DON_T_EXIST);
        }

        MemberOrderCommentDO memberOrderCommentDO = memberOrderCommentRepository.findById(orderProductCommentDO.getOrder().getId()).orElse(null);
        if (Objects.isNull(memberOrderCommentDO)) {
            return Wrapper.fail(ResponseCode.MC_MS_RECORDS_DON_T_EXIST);
        }

        MemberTradeCommentSubmitDetailVO submitDetailVO = new MemberTradeCommentSubmitDetailVO();
        submitDetailVO.setOrderProductId(memberTradeCommentSaveVO.getOrderProductId());
        submitDetailVO.setStar(memberTradeCommentSaveVO.getStar());
        submitDetailVO.setComment(memberTradeCommentSaveVO.getComment());
        submitDetailVO.setPics(memberTradeCommentSaveVO.getPics());

        this.baseSubmitMemberTradeComment(MemberTradeTypeEnum.BUYER, memberOrderCommentDO, Collections.singletonList(submitDetailVO), true);
        return Wrapper.success();
    }

    @Override
    public Wrapper<PageData<MobileCompleteCommentPageVO>> pageCompleteOrderComment(UserLoginCacheDTO loginUser, PageVO pageVO) {
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize(), Sort.by("createTime").descending());

        Page<MemberTradeCommentHistoryDO> pageList = memberTradeCommentHistoryRepository.findAll((Specification<MemberTradeCommentHistoryDO>) (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            predicateList.add(cb.equal(root.get("memberId"), loginUser.getMemberId()));
            predicateList.add(cb.equal(root.get("roleId"), loginUser.getMemberRoleId()));
            predicateList.add(cb.equal(root.get("tradeType"), MemberTradeTypeEnum.BUYER.getTypeEnum()));

            // app只显示非合同类型的订单
            List<Integer> orderTypeList = Arrays.asList(
                    OrderTypeEnum.INQUIRY_TO_PURCHASE.getCode(),
                    OrderTypeEnum.NEED_TO_PURCHASE.getCode(),
                    OrderTypeEnum.SPOT_PURCHASING.getCode(),
                    OrderTypeEnum.COLLECTIVE_PURCHASE.getCode(),
                    OrderTypeEnum.CHANNEL_STRAIGHT_MINING.getCode(),
                    OrderTypeEnum.CHANNEL_SPOT.getCode(),
                    OrderTypeEnum.CREDITS_EXCHANGE.getCode(),
                    OrderTypeEnum.CHANNEL_POINT_EXCHANGE.getCode());
            predicateList.add(cb.in(root.get("orderType")).value(orderTypeList));

            // 最近一年的数据
            predicateList.add(cb.greaterThanOrEqualTo(root.get("createTime"), LocalDateTime.now().minusYears(1L)));

            Predicate[] p = new Predicate[predicateList.size()];
            return query.where(predicateList.toArray(p)).getRestriction();
        }, page);

        // 被评价会员列表
        Set<MemberAndRoleIdBO> memberRoleSet = pageList.stream().map(e -> new MemberAndRoleIdBO(e.getSubMemberId(), e.getSubRoleId())).collect(Collectors.toSet());
        // 上级渠道
        Specification<MemberRelationDO> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();

            list.add(criteriaBuilder.equal(root.get("relType").as(Long.class), MemberRelationTypeEnum.OTHER.getCode()));

            for (MemberAndRoleIdBO memberAndRoleIdBO : memberRoleSet) {
                list.add(criteriaBuilder.and(
                        criteriaBuilder.or(criteriaBuilder.equal(root.get("subMemberId").as(Long.class), memberAndRoleIdBO.getMemberId())),
                        criteriaBuilder.or(criteriaBuilder.equal(root.get("subRoleId").as(Long.class), memberAndRoleIdBO.getRoleId()))
                ));
            }

            Join<Object, Object> roleJoin = root.join("role", JoinType.LEFT);
            list.add(criteriaBuilder.or(criteriaBuilder.equal(roleJoin.get("memberType").get("typeEnum").as(Integer.class), MemberTypeEnum.CHANNEL.getCode()), criteriaBuilder.equal(roleJoin.get("memberType").get("typeEnum").as(Integer.class), MemberTypeEnum.CHANNEL_PERSONAL.getCode())));

            Predicate[] p = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(p));
        };

        // 店铺头像map
        List<Long> orderIds = pageList.stream().map(MemberTradeCommentHistoryDO::getOrderId).collect(Collectors.toList());
        Map<Long, String> memberShopLogoMap = getMemberShopLogoMap(orderIds);

        List<MemberRelationDO> memberRelationDOList = memberRelationRepository.findAll(specification);
        Map<MemberAndRoleIdBO, MemberRelationDO> memberMap = memberRelationDOList.stream().collect(Collectors.toMap(e -> new MemberAndRoleIdBO(e.getSubMemberId(), e.getSubRoleId()), e -> e, (e1, e2) -> e2));
        List<MobileCompleteCommentPageVO> resultList = pageList.stream().map(e -> {
            MobileCompleteCommentPageVO mobileTradeCommentPageVO = new MobileCompleteCommentPageVO();
            mobileTradeCommentPageVO.setId(e.getId());
            mobileTradeCommentPageVO.setStar(e.getStar());
            mobileTradeCommentPageVO.setComment(e.getComment());
            mobileTradeCommentPageVO.setShopId(e.getShopId());
            mobileTradeCommentPageVO.setProductId(e.getProductId());
            mobileTradeCommentPageVO.setSkuId(e.getSkuId());
            mobileTradeCommentPageVO.setProduct(e.getProduct());
            mobileTradeCommentPageVO.setOrderId(e.getOrderId());
            mobileTradeCommentPageVO.setOrderType(e.getOrderType());
            mobileTradeCommentPageVO.setOrderNo(e.getOrderNo());
            mobileTradeCommentPageVO.setDealTime(e.getDealTime());
            mobileTradeCommentPageVO.setPrice(e.getPrice());
            mobileTradeCommentPageVO.setTotalPrice(e.getTotalPrice());
            mobileTradeCommentPageVO.setPics(e.getPics());
            mobileTradeCommentPageVO.setProductImgUrl(e.getProductImgUrl());
            mobileTradeCommentPageVO.setDealCount(e.getDealCount());
            mobileTradeCommentPageVO.setUnit(e.getUnit());
            mobileTradeCommentPageVO.setReplyContent(e.getReplyContent());

            if (MemberTypeEnum.MERCHANT.getCode().equals(loginUser.getMemberType())
                    || MemberTypeEnum.MERCHANT_PERSONAL.getCode().equals(loginUser.getMemberType())) {
                // 企业, 个人
                mobileTradeCommentPageVO.setSubMemberName(e.getSubMemberName());
                String logo = memberShopLogoMap.get(e.getOrderId());
                mobileTradeCommentPageVO.setSubMemberLogo(StringUtils.isNotEmpty(logo) ? logo : "");
            } else if (MemberTypeEnum.CHANNEL.getCode().equals(loginUser.getMemberType())
                    || MemberTypeEnum.CHANNEL_PERSONAL.getCode().equals(loginUser.getMemberType())) {
                // 渠道企业,渠道个人
                MemberRelationDO memberRelationDO = memberMap.get(new MemberAndRoleIdBO(e.getSubMemberId(), e.getSubRoleId()));
                if (Objects.nonNull(memberRelationDO)) {
                    // 上级渠道会员名称和logo
                    mobileTradeCommentPageVO.setSubMemberName(memberRelationDO.getMember().getName());
                    mobileTradeCommentPageVO.setSubMemberLogo(memberRelationDO.getMember().getLogo());
                }
            }
            return mobileTradeCommentPageVO;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<MobileCommentHistoryVO> getMemberCommentHistory(UserLoginCacheDTO loginUser, CommonIdVO commonIdVO) {
        MemberTradeCommentHistoryDO commentHistoryDO = memberTradeCommentHistoryRepository.findById(commonIdVO.getId()).orElse(null);
        if (Objects.isNull(commentHistoryDO)) {
            return Wrapper.fail(ResponseCode.MC_MS_RECORDS_DON_T_EXIST);
        }

        MobileCommentHistoryVO commentOrderProductVO = new MobileCommentHistoryVO();
        commentOrderProductVO.setId(commentHistoryDO.getId());
        commentOrderProductVO.setOrderProductId(commentHistoryDO.getOrderProductId());
        commentOrderProductVO.setOrderProductId(commentHistoryDO.getOrderProductId());
        commentOrderProductVO.setProductId(commentHistoryDO.getProductId());
        commentOrderProductVO.setSkuId(commentHistoryDO.getSkuId());
        commentOrderProductVO.setProductNo(commentHistoryDO.getOrderNo());
        commentOrderProductVO.setName(commentHistoryDO.getProduct());
        commentOrderProductVO.setCategory(commentHistoryDO.getCategory());
        commentOrderProductVO.setBrand(commentHistoryDO.getBrand());
        commentOrderProductVO.setSpec(commentHistoryDO.getSpec());
        commentOrderProductVO.setUnit(commentHistoryDO.getUnit());
        commentOrderProductVO.setLogo(commentHistoryDO.getProductImgUrl());
        commentOrderProductVO.setPrice(commentHistoryDO.getPrice());
        commentOrderProductVO.setQuantity(commentHistoryDO.getDealCount());
        commentOrderProductVO.setAmount(commentHistoryDO.getTotalPrice());

        return Wrapper.success(commentOrderProductVO);
    }

    @Override
    public Wrapper<PageData<MobileReceiveCommentPageVO>> pageReceiveOrderComment(UserLoginCacheDTO loginUser, PageVO pageVO) {
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize(), Sort.by("createTime").descending());

        Page<MemberTradeCommentHistoryDO> pageList = memberTradeCommentHistoryRepository.findAll((Specification<MemberTradeCommentHistoryDO>) (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            predicateList.add(cb.equal(root.get("subMemberId"), loginUser.getMemberId()));
            predicateList.add(cb.equal(root.get("subRoleId"), loginUser.getMemberRoleId()));
            predicateList.add(cb.equal(root.get("tradeType"), MemberTradeTypeEnum.SELLER.getTypeEnum()));

            // app只显示非合同类型的订单
            List<Integer> orderTypeList = Arrays.asList(
                    OrderTypeEnum.INQUIRY_TO_PURCHASE.getCode(),
                    OrderTypeEnum.NEED_TO_PURCHASE.getCode(),
                    OrderTypeEnum.SPOT_PURCHASING.getCode(),
                    OrderTypeEnum.COLLECTIVE_PURCHASE.getCode(),
                    OrderTypeEnum.CHANNEL_STRAIGHT_MINING.getCode(),
                    OrderTypeEnum.CHANNEL_SPOT.getCode(),
                    OrderTypeEnum.CREDITS_EXCHANGE.getCode(),
                    OrderTypeEnum.CHANNEL_POINT_EXCHANGE.getCode());
            predicateList.add(cb.in(root.get("orderType")).value(orderTypeList));

            // 最近一年的数据
            predicateList.add(cb.greaterThanOrEqualTo(root.get("createTime"), LocalDateTime.now().minusYears(1L)));

            Predicate[] p = new Predicate[predicateList.size()];
            return query.where(predicateList.toArray(p)).getRestriction();
        }, page);

        // 评价会员列表
        Set<MemberAndRoleIdBO> memberRoleSet = pageList.stream().map(e -> new MemberAndRoleIdBO(e.getMemberId(), e.getRoleId())).collect(Collectors.toSet());
        // 上级渠道
        Specification<MemberRelationDO> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();

            list.add(criteriaBuilder.equal(root.get("relType").as(Long.class), MemberRelationTypeEnum.OTHER.getCode()));

            for (MemberAndRoleIdBO memberAndRoleIdBO : memberRoleSet) {
                list.add(criteriaBuilder.and(
                        criteriaBuilder.or(criteriaBuilder.equal(root.get("subMemberId").as(Long.class), memberAndRoleIdBO.getMemberId())),
                        criteriaBuilder.or(criteriaBuilder.equal(root.get("subRoleId").as(Long.class), memberAndRoleIdBO.getRoleId()))
                ));
            }

            Join<Object, Object> roleJoin = root.join("role", JoinType.LEFT);
            list.add(criteriaBuilder.or(criteriaBuilder.equal(roleJoin.get("memberType").get("typeEnum").as(Integer.class), MemberTypeEnum.CHANNEL.getCode()), criteriaBuilder.equal(roleJoin.get("memberType").get("typeEnum").as(Integer.class), MemberTypeEnum.CHANNEL_PERSONAL.getCode())));

            Predicate[] p = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(p));
        };
        List<MemberRelationDO> memberRelationDOList = memberRelationRepository.findAll(specification);
        Map<MemberAndRoleIdBO, MemberRelationDO> memberMap = memberRelationDOList.stream().collect(Collectors.toMap(e -> new MemberAndRoleIdBO(e.getSubMemberId(), e.getSubRoleId()), e -> e, (e1, e2) -> e2));

        // 店铺头像map
        List<Long> orderIds = pageList.stream().map(MemberTradeCommentHistoryDO::getOrderId).collect(Collectors.toList());
        Map<Long, String> memberShopLogoMap = getMemberShopLogoMap(orderIds);

        List<MobileReceiveCommentPageVO> resultList = pageList.stream().map(e -> {
            MobileReceiveCommentPageVO mobileTradeCommentPageVO = new MobileReceiveCommentPageVO();
            mobileTradeCommentPageVO.setId(e.getId());
            mobileTradeCommentPageVO.setStar(e.getStar());
            mobileTradeCommentPageVO.setComment(e.getComment());
            mobileTradeCommentPageVO.setShopId(e.getShopId());
            mobileTradeCommentPageVO.setProductId(e.getProductId());
            mobileTradeCommentPageVO.setSkuId(e.getSkuId());
            mobileTradeCommentPageVO.setProduct(e.getProduct());
            mobileTradeCommentPageVO.setOrderId(e.getOrderId());
            mobileTradeCommentPageVO.setOrderType(e.getOrderType());
            mobileTradeCommentPageVO.setOrderNo(e.getOrderNo());
            mobileTradeCommentPageVO.setDealTime(e.getDealTime());
            mobileTradeCommentPageVO.setPrice(e.getPrice());
            mobileTradeCommentPageVO.setTotalPrice(e.getTotalPrice());
            mobileTradeCommentPageVO.setPics(e.getPics());
            mobileTradeCommentPageVO.setProductImgUrl(e.getProductImgUrl());
            mobileTradeCommentPageVO.setDealCount(e.getDealCount());
            mobileTradeCommentPageVO.setUnit(e.getUnit());

            if (MemberTypeEnum.MERCHANT.getCode().equals(loginUser.getMemberType())
                    || MemberTypeEnum.MERCHANT_PERSONAL.getCode().equals(loginUser.getMemberType())) {
                // 企业, 个人
                mobileTradeCommentPageVO.setMemberName(e.getMemberName());
                String logo = memberShopLogoMap.get(e.getOrderId());
                mobileTradeCommentPageVO.setMemberLogo(StringUtils.isNotEmpty(logo) ? logo : "");

            } else if (MemberTypeEnum.CHANNEL.getCode().equals(loginUser.getMemberType())
                    || MemberTypeEnum.CHANNEL_PERSONAL.getCode().equals(loginUser.getMemberType())) {
                // 渠道企业,渠道个人
                MemberRelationDO memberRelationDO = memberMap.get(new MemberAndRoleIdBO(e.getMemberId(), e.getRoleId()));
                if (Objects.nonNull(memberRelationDO)) {
                    // 上级渠道会员名称和logo
                    mobileTradeCommentPageVO.setMemberName(memberRelationDO.getMember().getName());
                    mobileTradeCommentPageVO.setMemberLogo(memberRelationDO.getMember().getLogo());
                }
            }
            return mobileTradeCommentPageVO;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    /**
     * 获取店铺logoMap
     * @param orderIds 订单ID
     * @return 返回结果
     */
    private Map<Long, String> getMemberShopLogoMap(List<Long> orderIds) {
        List<OrderVendorLogoFeignVO> shopListInnerVOS = Collections.emptyList();
        OrderFeignIdsVO feignVO = new OrderFeignIdsVO();
        feignVO.setOrderIds(orderIds);
        Wrapper<List<OrderVendorLogoFeignVO>> listWrapper = orderFeignService.findOrderVendorLogo(feignVO);
        if (ResponseCode.SUCCESS.getCode() == listWrapper.getCode()) {
            shopListInnerVOS = listWrapper.getData();
        }

        return shopListInnerVOS.stream().peek(vo -> {
            if (StrUtil.isBlank(vo.getLogo())) {
                vo.setLogo("");
            }
        }).collect(Collectors.toMap(OrderVendorLogoFeignVO::getOrderId, OrderVendorLogoFeignVO::getLogo, (e1, e2) -> e2));
    }
}
